<?php
// +----------------------------------------------------------------------
// | yian [ 壹乘加工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 张依然 <439583514@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// |标签库
// +----------------------------------------------------------------------
namespace app\ads\taglib;

use think\Db;

class AdsTagLib
{
    /**
     * 栏目标签
     * Array ( [module] => ads [action] => lists [position_id] => 1 [num] => 10 [cat_id] => 0 [order] => listorder desc [page] => 0 )
     */
    public function lists($data)
    {
        $def_where=['is_show'=>1];
        $where = isset($data['where']) ? $data['where'] : $def_where;
        $order = isset($data['order']) ? $data['order'] : 'listorder,id desc';
        $cat_id = isset($data['cat_id']) ? $data['cat_id'] : 0;
        $num = isset($data['num']) ? (int) $data['num'] : 10;
        $position_id = isset($data['position_id']) ? (int) $data['position_id'] : 0;
        $siteid = isset($data['siteid']) ? (int) $data['siteid'] : 1;

        if(empty($position_id)){
            return array();
        }
        $where['siteid']=$siteid;
        $where['position_id']=$position_id;
        $wheres= !empty($cat_id)?"find_in_set($cat_id,cat_id)":true;

       $ads_position_res= Db::name('ads_position')->where(['position_id'=>$position_id,'is_show'=>0,'siteid'=>$siteid])->find();
       if(!empty($ads_position_res)){
           return array();
       }

        $result = Db::name('ads')
            ->where($where)
            ->where($wheres)
            ->whereTime('start_time', '<=', time())
            ->whereTime('end_time', '>=', time())
            ->limit($num)->order($order)
            ->withAttr('thumb', function ($value, $data) {
            return get_file_path($value);
        })->withAttr('m_thumb', function ($value, $data) {
            return get_file_path($value);
        })->select();




        return $result;
    }

}
